package com.hashcoding.eyes.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hashcoding.eyes.R;
import com.hashcoding.eyes.data.constant.APIConstant;
import com.hashcoding.eyes.data.entity.Photo;
import com.hashcoding.eyes.data.entity.PhotosList;
import com.hashcoding.eyes.data.entity.User;
import com.hashcoding.eyes.module.utils.LogUtil;
import com.hashcoding.eyes.module.utils.ResourceUtil;
import com.hashcoding.eyes.ui.adapter.BaseListAdapter;
import com.hashcoding.eyes.ui.adapter.PhotosListAdapter;
import com.hashcoding.eyes.ui.protocol.IScrollListener;
import com.hashcoding.eyes.ui.view.EmptyLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FavoritedActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,
        AdapterView.OnItemClickListener {

    private final static String KEY_USER = "user";

    @Bind(R.id.error_layout)
    EmptyLayout mErrorLayout;
    @Bind(R.id.list_view)
    ListView mListView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout mRefreshLayout;

    private User mUser;
    private PhotosListAdapter mAdapter;
    private int mLastID;

    public static void launch(Context context, User user) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_USER, user);

        Intent intent = new Intent(context, FavoritedActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorited);
        ButterKnife.bind(this);

        mUser = getIntent().getParcelableExtra(KEY_USER);

        initView();
    }

    private void initView() {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setColorSchemeColors(
                ResourceUtil.getColorsByStringArray(this, R.array.ProgressColorArray));

        mErrorLayout.setState(EmptyLayout.STATE_LOADING);
        mErrorLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshData();
            }
        });

        mAdapter = new PhotosListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        mListView.setOnScrollListener(new IScrollListener() {
            @Override
            public void onLoadMore() {
                if (mAdapter.canLoadMore()) {
                    mAdapter.setState(BaseListAdapter.STATE_LOADING);
                    loadMore();
                }
            }
        });
        mAdapter.setList(new ArrayList<Photo>());

        refreshData();
    }

    private void refreshData() {

        mRefreshLayout.setRefreshing(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // load data
                List<Photo> photos = PhotosList.instance().loadData();
                if (photos.size() == 0) {
                    mErrorLayout.setState(EmptyLayout.STATE_EMPTY);
                    mAdapter.setState(BaseListAdapter.STATE_NO_DATA);
                } else {
                    mAdapter.setList(photos);
                    mErrorLayout.setState(EmptyLayout.STATE_HIDE);
                    mAdapter.setState(photos.size() >= APIConstant.PAGE_COUNT
                            ? BaseListAdapter.STATE_LOAD_MORE
                            : BaseListAdapter.STATE_NO_MORE);
                    mLastID = photos.get(photos.size() - 1).id;
                }
                mRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }

    private void loadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<Photo> photos = PhotosList.instance().loadData(mLastID);
                mAdapter.addList(photos);
                mAdapter.setState(photos.size() >= APIConstant.PAGE_COUNT
                        ? BaseListAdapter.STATE_LOAD_MORE
                        : BaseListAdapter.STATE_NO_MORE);
                mLastID = photos.get(photos.size() - 1).id;
            }
        }, 2000);
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PhotoActivity.launch(this, mAdapter.getItem(position));
    }

    @OnClick(R.id.title_left_btn)
    protected void onTitleLeftClick() {
        onBackPressed();
    }
}
